<?php

namespace app\common\model;

use app\common\model\goods\Item;
use think\Model;


class Cart extends Model
{





    // 表名
    protected $name = 'cart';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'flag'
    ];

    public static function getflagAttr($valu, $data)
    {
        return false;
    }
    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function item()
    {
        return $this->belongsTo('app\common\model\goods\Item', 'item_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public static function addCart($param)
    {
        $goods = Goods::where('id', $param['goods_id'])->find();
        $suk = Item::where('goods_id', $param['goods_id'])->where('id', $param['item_id'])->find();
        if (!$goods || !$suk) {
            exception('商品不存在');
        }
        $model = new self;
        $cart = $model->where('goods_id', $param['goods_id'])->where('item_id', $param['item_id'])->where('user_id', $param['uid'])->where('type', $param['type'])->find();
        if ($cart) {
            if ($cart['num'] <= 0) {
                $model->delete();
            } else {
                $cart->allowField(true)->save(['num' => $param['num']]);
            }
            return  $cart->id;
        } else {
            $data = [
                'user_id' => $param['uid'],
                'goods_id' => $param['goods_id'],
                'num' => $param['num'],
                'item_id' => $param['item_id'],
                'type' => $param['type'],
            ];
            if ($param['num'] > 0) {
                $model->allowField(true)->save($data);
                return  $model->id;
            }
        }
        exception('失败');
    }

    //删除购物车
    public static function delCart($uid, $ids)
    {
        $model = new self;
        return $model->where('user_id', $uid)->whereIn('id', $ids)->delete();
    }


    //获取购物车下单商品信息
    public static function getGoods($ids, $param)
    {
        $cart_goods = Cart::with(['goods', 'item'])->whereIn('cart.id', $ids)->where('user_id', $param['uid'])->where('type', $param['type'])->select();
        $user = User::get($param['uid']);
        foreach ($cart_goods as &$v) {
            if ($v->goods->stock < $v->num) {
                exception('商品【' . $v->goods->title . '】库存不足，不能下单');
            }
            if ($v->goods->status == '0') {
                exception('商品【' . $v->goods->title . '】已下架，不能下单');
            }
            if ($v->goods->deletetime != null) {
                exception('商品【' . $v->goods->title . '】已删除，不能下单');
            }
            $v->money = $v->num * $v->item->price;
            $v->inside_price = $v->num * $v->item->inside_price;
            $v->total_integral = $v->num * $v->goods->integral;
            if (isset($user['group_id']) && $user['group_id'] == 2) {
                $v->current_price = $v->item->inside_price;
            } else {
                $v->current_price = $v->item->price;
            }
        }
        return $cart_goods;
    }
}
